package appfight.level;

import java.util.ArrayList;
import java.util.Random;

import org.anddev.andengine.entity.IEntity;
import org.anddev.andengine.entity.modifier.AlphaModifier;
import org.anddev.andengine.entity.modifier.ColorModifier;
import org.anddev.andengine.entity.modifier.DelayModifier;
import org.anddev.andengine.entity.modifier.IEntityModifier.IEntityModifierListener;
import org.anddev.andengine.entity.modifier.LoopEntityModifier;
import org.anddev.andengine.entity.modifier.MoveModifier;
import org.anddev.andengine.entity.modifier.ParallelEntityModifier;
import org.anddev.andengine.entity.modifier.RotationModifier;
import org.anddev.andengine.entity.modifier.ScaleModifier;
import org.anddev.andengine.entity.modifier.SequenceEntityModifier;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.util.modifier.IModifier;

import braindamagestudio.com.app.fight.MainGameActivity;

import android.content.Intent;
import appfight.icon.manager.IconManager;
import appfight.icon.manager.IconManager.IconQueryResult;
import appfight.importApp.ImportAppMenu;

import appfight.icon.manager.IconManager;
import appfight.monster.Monster;

public class GameLevelStartAnimation extends GameLevel {
	
	ArrayList<Sprite> iconList = new ArrayList<Sprite>();
	IconQueryResult iconsInfo;
	
	Sprite _background;
	AnimatedSprite _girl;
	Sprite girl_app;
	Sprite angryApp;
	@Override
	public void LevelDesign()
	{
		_activity.PlayMusic(MainGameActivity.MUSIC_OPENNING, true);
		LoadPlayerSelectIcon();
		
		_background = new Sprite(0,0,_resoruceManager.IntelligentLoadTextureRegion("grass_background.png"));
		_background.registerEntityModifier(
				
				new SequenceEntityModifier(
				new MoveModifier(10,0,-500, 0, 0)				
						)	
				);
		_sceneManager.attach(_background);

		Sprite house = new Sprite(650,150,_resoruceManager.IntelligentLoadTextureRegion("house.png"));
		house.setScale(1.5f);
		
		_background.attachChild(house);
		InitCow();
		InitIconFamily();
		
		InitAngryApp();
		InitGirl();
		
		
	}

	private void InitCow()
	{
		
		Random m = new Random();
		for(int i=0;i<20;i++)
		{
			int randomInt = m.nextInt() % 50;
			
			final Sprite cow = new Sprite(500 + randomInt * (i % 4) ,300 + randomInt/2 * (i % 3),_resoruceManager.IntelligentLoadIconTextureRegion(iconsInfo.Paths.get(0)));

			Jump(cow);
			_background.attachChild(cow);
			
		}
	}
	
	private void Jump(final Sprite target)
	{

		ParallelEntityModifier jumpUp = new ParallelEntityModifier(
				new MoveModifier(1,target.getX(),target.getX() - 5,target.getY(),target.getY()-50),
				new ScaleModifier(1,1f,1f, 0.8f, 1.2f)
		);
		ParallelEntityModifier jumpDown = new ParallelEntityModifier(
				new MoveModifier(1,target.getX() - 5,target.getX() - 10,target.getY()-50,target.getY()),
				new ScaleModifier(1,1f,1f, 1.2f, 0.8f)
		);

		final SequenceEntityModifier jumpModifier = new SequenceEntityModifier(jumpUp,jumpDown);
		IEntityModifierListener loopJump = new IEntityModifierListener()
		{

				@Override
				public void onModifierStarted(IModifier<IEntity> pModifier,	IEntity pItem) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem) {
					// TODO Auto-generated method stub
					Jump(target);
				}					
		};
		jumpModifier.addModifierListener(loopJump);
		target.registerEntityModifier(jumpModifier);
	}
	
	private void InitIconFamily()
	{

		final Sprite father = new Sprite(300,600,_resoruceManager.IntelligentLoadIconTextureRegion(iconsInfo.Paths.get(1)));
		final Sprite mother = new Sprite(330,605,_resoruceManager.IntelligentLoadIconTextureRegion(iconsInfo.Paths.get(2)));
		Sprite child = new Sprite(316,620,_resoruceManager.IntelligentLoadIconTextureRegion(iconsInfo.Paths.get(1)));
		
		father.setScale(2f);
		mother.setScale(2f);
		child.setScale(1.2f);
		
		child.registerEntityModifier(
				
				new SequenceEntityModifier(		
					new IEntityModifierListener()
					{

							@Override
							public void onModifierStarted(IModifier<IEntity> pModifier,	IEntity pItem) {
								// TODO Auto-generated method stub
								
							}

							@Override
							public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem) {
								// TODO Auto-generated method stub
								father.registerEntityModifier
								(
									new SequenceEntityModifier
									(
										new MoveModifier(1,300,310,600,605),
										new MoveModifier(1,310,300,605,600)
									)
								);
								mother.registerEntityModifier
								(
										new SequenceEntityModifier
										(
											new MoveModifier(1,330,320,605,610),
											new MoveModifier(1,320,330,610,605)
										)
								);
							}					
					} 
					,
					new DelayModifier(0.5f),
					new SequenceEntityModifier(
						new RotationModifier(0.5f, 5, -5),
						new RotationModifier(0.5f, -5, 5),
						new ParallelEntityModifier(
								new MoveModifier(1,316,316,620,600),
								new ScaleModifier(1,1f,1f, 0.8f, 1.2f)
						),
						new ParallelEntityModifier(
								new MoveModifier(1,316,316,600,620),
								new ScaleModifier(1,1f,1f, 1.2f, 0.8f)
						),
						new ScaleModifier(0.4f,1f,1f, 1.2f, 1f)
					)
				)
		);

		_background.attachChild(father);
		_background.attachChild(mother);
		_background.attachChild(child);
		
	}

	private void InitAngryApp()
	{
		angryApp = new Sprite(-100,-100,_resoruceManager.IntelligentLoadIconTextureRegion(iconsInfo.Paths.get(4)));
		angryApp.setColor(1, 1f, 1f);
		

		ParallelEntityModifier angryUp = new ParallelEntityModifier(
				new ScaleModifier(1,1,2),
				new ColorModifier(1, 1, 1f, 1f, 0.5f, 1f, 0.5f)
		);
		
		DelayModifier delay = new DelayModifier(8);
		DelayModifier delay2 = new DelayModifier(3);
		
		MoveModifier popUp = new MoveModifier(2,900,800,600,400);
		

		IEntityModifierListener musicChange = new IEntityModifierListener()
		{

				@Override
				public void onModifierStarted(IModifier<IEntity> pModifier,	IEntity pItem) {
					// TODO Auto-generated method stub

				}

				@Override
				public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem) {
					// TODO Auto-generated method stub
					_girl.stopAnimation();
					_girl.setCurrentTileIndex(4);
					Suprise();
				}					
		};

		IEntityModifierListener moreAngry = new IEntityModifierListener()
		{

				@Override
				public void onModifierStarted(IModifier<IEntity> pModifier,	IEntity pItem) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem) {
					// TODO Auto-generated method stub
					girl_app.clearEntityModifiers();
					BigSuprise();
				}					
		};
		
		popUp.addModifierListener(musicChange);
		angryUp.addModifierListener(moreAngry);
		
		final SequenceEntityModifier angryAppModifier = new SequenceEntityModifier(delay,popUp,delay2,angryUp);
		
		angryApp.registerEntityModifier(angryAppModifier);
		
		_background.attachChild(angryApp);
		
	}
	
	private void InitGirl()
	{
		girl_app = new Sprite(-20,10,_resoruceManager.IntelligentLoadIconTextureRegion(iconsInfo.Paths.get(3)));
		girl_app.setScale(0.5f);
		
		girl_app.registerEntityModifier
		(
			new LoopEntityModifier(
				new SequenceEntityModifier
				(
					new RotationModifier(0.5f, 10, -10),
					new RotationModifier(0.5f, -10, 10)
				)
			)
		);
		
		MoveModifier walk = new MoveModifier(10,100,600,400,400);
		
		SequenceEntityModifier girlModifier = new SequenceEntityModifier(walk);

		_girl = new AnimatedSprite(100,400,_resoruceManager.IntelligentLoadTiledTextureRegion("character2.png"));
		_girl.setScale(2f);
		_girl.animate(new long[] { 200, 200,200 }, 3, 5, true);
		_girl.attachChild(girl_app);
		_girl.registerEntityModifier(girlModifier);
		
		_background.attachChild(_girl);
		
	}
	
	private void Suprise()
	{
		final Sprite supriseSign = new Sprite(0,0,_resoruceManager.IntelligentLoadTextureRegion("supriseSign.png"));

		_background.detachChild(_girl);
		_background.attachChild(supriseSign);
		_background.attachChild(_girl);
		
		MoveModifier popUp = new MoveModifier(0.5f,590,590,400,360);
		ScaleModifier longerX = new ScaleModifier(0.5f,1,1.5f,1,1);
		ScaleModifier recoverX = new ScaleModifier(0.25f,1.5f,1,1,1);
		ScaleModifier faterY = new ScaleModifier(0.25f,1,1,1,1.5f);
		ScaleModifier recoverY = new ScaleModifier(0.25f,1,1,1.5f,1);
		
		IEntityModifierListener destroySign = new IEntityModifierListener()
		{

				@Override
				public void onModifierStarted(IModifier<IEntity> pModifier,	IEntity pItem) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem) {
					// TODO Auto-generated method stub
					supriseSign.setPosition(0, 0);
				}					
		};
		
		SequenceEntityModifier supriseModifier = new SequenceEntityModifier(popUp,longerX,recoverX,faterY,recoverY);
		supriseModifier.addModifierListener(destroySign);
		supriseSign.registerEntityModifier(supriseModifier);
		
	}
		
	private void BigSuprise()
	{
		final Sprite supriseSign1 = new Sprite(0,0,_resoruceManager.IntelligentLoadTextureRegion("supriseSign.png"));
		final Sprite supriseSign2 = new Sprite(5,0,_resoruceManager.IntelligentLoadTextureRegion("supriseSign.png"));
		final Sprite supriseSign3 = new Sprite(10,0,_resoruceManager.IntelligentLoadTextureRegion("supriseSign.png"));
		
		supriseSign1.attachChild(supriseSign2);
		supriseSign1.attachChild(supriseSign3);

		_background.attachChild(supriseSign1);
		
		MoveModifier popUp = new MoveModifier(0.5f,590,590,400,360);

		IEntityModifierListener destroySign = new IEntityModifierListener()
		{

				@Override
				public void onModifierStarted(IModifier<IEntity> pModifier,	IEntity pItem) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem) {
					// TODO Auto-generated method stub
					_background.detachChild(supriseSign1);
					Conversation();
					StartBattle();
				}					
		};
		
		popUp.addModifierListener(destroySign);
		supriseSign1.registerEntityModifier(popUp);
	}
	
	private void Conversation(){}
	
	private void StartBattle()
	{
		_background.detachChild(_girl);
		_background.attachChild(_girl);

		IEntityModifierListener attacked = new IEntityModifierListener()
		{

				@Override
				public void onModifierStarted(IModifier<IEntity> pModifier,	IEntity pItem) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem) {
					// TODO Auto-generated method stub
					ShowAttackEffect();
				}					
		};
		
		RotationModifier rotateModifier = new RotationModifier(0.3f,girl_app.getRotation(),10);
		MoveModifier moveModifier = new MoveModifier(0.5f,girl_app.getX(),girl_app.getX() + 150,girl_app.getY(),girl_app.getY());
		
		SequenceEntityModifier attackModifier = new SequenceEntityModifier(rotateModifier,moveModifier);
		attackModifier.addModifierListener(attacked);
		
		girl_app.registerEntityModifier(attackModifier);
		
	}
	
	private void ShowAttackEffect()
	{
		final AnimatedSprite crawEffect = new AnimatedSprite(770,390,_resoruceManager.IntelligentLoadTiledTextureRegion("crawEffect.png"));
		
		crawEffect.animate(50, 5);
		
		DelayModifier count = new DelayModifier(1);
		
		IEntityModifierListener countEvent = new IEntityModifierListener()
		{

				@Override
				public void onModifierStarted(IModifier<IEntity> pModifier,	IEntity pItem) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem) {
					// TODO Auto-generated method stub
					crawEffect.setPosition(0, 0);
					EnamyDown();
				}					
		};
		
		count.addModifierListener(countEvent);
		
		crawEffect.registerEntityModifier(count);
		
		_background.attachChild(crawEffect);
		
	}
	
	private void EnamyDown()
	{

		ScaleModifier scaleRecover = new ScaleModifier(0.5f,2,0.7f); 
		ColorModifier angryAppDispear = new ColorModifier(1f,1f,0f,1f,0f,1f,0f);
		AlphaModifier alphaModifier = new AlphaModifier(1f,1,0); 
		
		ParallelEntityModifier appDown = new ParallelEntityModifier(scaleRecover,angryAppDispear,alphaModifier);

		IEntityModifierListener walkToHouse = new IEntityModifierListener()
		{

				@Override
				public void onModifierStarted(IModifier<IEntity> pModifier,	IEntity pItem) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem) {
					// TODO Auto-generated method stub
					WalkToHouse();
				}					
		};
		
		appDown.addModifierListener(walkToHouse);
		
		this.angryApp.registerEntityModifier(appDown);
	}
	
	private void WalkToHouse()
	{
		MoveModifier girlMove1 = new MoveModifier(1f,600,675,400,400);
		MoveModifier appMove = new MoveModifier(1f,girl_app.getX(),girl_app.getX()-150,girl_app.getY(),girl_app.getY());
		final MoveModifier girlMove2 = new MoveModifier(5f,675,675,400,0);
		final ScaleModifier girlScale = new ScaleModifier(5f,2,0.5f);
		final DelayModifier countChangeLevel = new DelayModifier(2f);
		
		
		

		_girl.animate(new long[] { 80, 80,80 }, 3, 5, true);
		
		girl_app.registerEntityModifier
		(
			new ParallelEntityModifier(
				new LoopEntityModifier(
					new SequenceEntityModifier
					(
						new RotationModifier(0.2f, 10, -10),
						new RotationModifier(0.2f, -10, 10)
					)
				),
				appMove
			)
		);
		
		IEntityModifierListener moveChange = new IEntityModifierListener()
		{

				@Override
				public void onModifierStarted(IModifier<IEntity> pModifier,	IEntity pItem) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem) {
					// TODO Auto-generated method stub
					_girl.animate(new long[] { 80, 80,80 }, 0, 2, true);
					ParallelEntityModifier nextMove = new ParallelEntityModifier(girlMove2,girlScale,countChangeLevel);
					_girl.registerEntityModifier(nextMove);
					girl_app.registerEntityModifier
					(
						new LoopEntityModifier(
							new SequenceEntityModifier
							(
								new RotationModifier(0.2f, 10, -10),
								new RotationModifier(0.2f, -10, 10)
							)
						)
					);
				}					
		};

		IEntityModifierListener levelChange = new IEntityModifierListener()
		{

				@Override
				public void onModifierStarted(IModifier<IEntity> pModifier,	IEntity pItem) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem) {
					// TODO Auto-generated method stub
					animateFinish();
				}					
		};
		
		countChangeLevel.addModifierListener(levelChange);
		
		girlMove1.addModifierListener(moveChange);
		_girl.registerEntityModifier(girlMove1);
		
		
	}
	
	private void animateFinish()
	{
		_activity.StopAllMusic();
		_activity.PlayMusic(MainGameActivity.MUSIC_TOWN, true);
		_activity.changedSceneCharacterX = 4;
		_activity.changedSceneCharacterY = 5;
		this.ChangeLevel("MainCharacterHouseSecondFloor");
		
	} 
	
	private void LoadPlayerSelectIcon()
	{
		IconManager iconManager = new IconManager(this._activity);
		
		iconsInfo = iconManager.getAllIconsPath();
		
		for(int i=0;i < 10;i++)
		{
			Sprite temp = new Sprite(32,32,_resoruceManager.IntelligentLoadIconTextureRegion(iconsInfo.Paths.get(i)));
			iconList.add(temp);
		}
	}
	
	
	/*IUpdateHandler h = new IUpdateHandler()
	{
		float count = 0;
		@Override
		public void onUpdate(float pSecondsElapsed) {
			// TODO Auto-generated method stub
			count += pSecondsElapsed;
			if(count>2)
			{
				//ChangeLevel("StartLevel");
			}
		}

		@Override
		public void reset() {
			// TODO Auto-generated method stub
			
		}

	};
//_sceneManager.registerUpdateHandler(h);
*/
}
